//
//  JYJSubscriptionCollectionViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/14.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJSubscriptionCollectionViewCell.h"
#import "BaseTableView.h"
#import "JYJSubscriptionTableViewCell.h"
#import "JYJTAMacro.h"
#import "JYJNetWorkParser.h"
#import "JYJTwoSegmentModel.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
@interface JYJSubscriptionCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *marrOfModel;
@property (nonatomic, strong)  UIImageView *imageView ;//未登录图片
@property (nonatomic, strong) UIImageView *image;//未收藏图片
@property (nonatomic, strong) UILabel *label;// 未收藏label
@end
@implementation JYJSubscriptionCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logout_bg_0"]];
        _imageView.frame = CGRectMake(0, 45, SCREEN_WIDTH, SCREEN_WIDTH - 40);
        [self.contentView addSubview:_imageView];
        
    }
    return self;
}
-(void)setDic:(NSDictionary *)dic {
    _dic = dic;
    [self.tableView reloadData];
    NSUserDefaults *userDeafults = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDeafults objectForKey:@"userName"];
    if (name != nil) {
        self.tableView.hidden = NO;
        self.imageView.hidden = YES;
        self.label.hidden = YES;
        self.image.hidden = YES;
       NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22";
        NSString *strUid = dic[@"session"][@"uid"];
        NSString *str2 = @"%22%2C%22sid%22%3A%22";
        NSString *strSid = dic[@"session"][@"sid"];
        NSString *str3 = @"%22%7D%2C%22cat_id%22%3A%220%22%2C%22target_id%22%3A%22";
        NSString *str4 = @"%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A2%2C%22searchtype%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
        str1 = [[[[[[str1 stringByAppendingString:strUid]stringByAppendingString:str2]stringByAppendingString:strSid]stringByAppendingString:str3]stringByAppendingString:strUid]stringByAppendingString:str4];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:str1 andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            self.marrOfModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            if (![result[@"paginated"][@"count"] isEqual:@0]) {
                for (NSDictionary *dic in dataArray) {
                    JYJTwoSegmentModel *model = [JYJTwoSegmentModel modelWithDic:dic];
                    [self.marrOfModel addObject:model];
                }
                [self.tableView reloadData];
                if (self.marrOfModel.count <= 5) {
                    self.tableView.scrollEnabled = NO;
                }else {
                    self.tableView.scrollEnabled = YES;
                }

                
            }else {
                self.tableView.hidden = YES;
                self.imageView.hidden = YES;
                self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
                _image.frame = CGRectMake(0, 0, 150,150);
                _image.center = self.contentView.center;
                [self.contentView addSubview:_image];
                self.label = [[UILabel alloc]init];
                _label.text = @"未搜到相关内容";
                _label.textAlignment = 1;
                _label.font = [UIFont systemFontOfSize:25];
                [self.contentView addSubview:_label];
                _label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
                [_label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView.mas_left).offset(100);
                    make.right.equalTo(self.contentView.mas_right).offset(-100);
                    make.bottom.equalTo(_image.mas_top).with.offset(20);
                    make.height.mas_equalTo(80);
                }];

            }
        } andFailureBlock:^(NSError *error) {
            
        }];
    }else {
        self.tableView.hidden = YES;
        self.label.hidden = YES;
        self.image.hidden = YES;
        self.imageView.hidden = NO;

    }
    
}
- (void)createTableView {
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[JYJSubscriptionTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 下拉刷新动画
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        NSString *name = [NSString stringWithFormat:@"list_load_%d",i];
        UIImage *image = [UIImage imageNamed:name];
        [marr addObject:image];
    }
    //    /** 普通闲置状态 */
    //    MJRefreshStateIdle = 1,
    //    /** 松开就可以进行刷新的状态 */
    //    MJRefreshStatePulling,
    //    /** 正在刷新中的状态 */
    //    MJRefreshStateRefreshing,
    
    [header setImages:marr forState:MJRefreshStateIdle];
    [header setImages:marr forState:MJRefreshStatePulling];
    [header setImages:marr forState: MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;

}
#pragma mark----------刷新数据
-(void)loadNewData {
    NSUserDefaults *userDeafults = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDeafults objectForKey:@"userName"];
    if (name != nil) {
        self.tableView.hidden = NO;
        self.imageView.hidden = YES;
        self.label.hidden = YES;
        self.image.hidden = YES;
        NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22";
        NSString *strUid = self.dic[@"session"][@"uid"];
        NSString *str2 = @"%22%2C%22sid%22%3A%22";
        NSString *strSid = self.dic[@"session"][@"sid"];
        NSString *str3 = @"%22%7D%2C%22cat_id%22%3A%220%22%2C%22target_id%22%3A%22";
        NSString *str4 = @"%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A2%2C%22searchtype%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
        str1 = [[[[[[str1 stringByAppendingString:strUid]stringByAppendingString:str2]stringByAppendingString:strSid]stringByAppendingString:str3]stringByAppendingString:strUid]stringByAppendingString:str4];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:str1 andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            self.marrOfModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            if (![result[@"paginated"][@"count"] isEqual:@0]) {
                for (NSDictionary *dic in dataArray) {
                    JYJTwoSegmentModel *model = [JYJTwoSegmentModel modelWithDic:dic];
                    [self.marrOfModel addObject:model];
                }
                [self.tableView reloadData];
                if (self.marrOfModel.count <= 5) {
                    self.tableView.scrollEnabled = NO;
                }else {
                    self.tableView.scrollEnabled = YES;
                }
                
                
            }else {
                self.tableView.hidden = YES;
                self.imageView.hidden = YES;
                self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
                _image.frame = CGRectMake(0, 0, 150,150);
                _image.center = self.contentView.center;
                [self.contentView addSubview:_image];
                self.label = [[UILabel alloc]init];
                _label.text = @"未搜到相关内容";
                _label.textAlignment = 1;
                _label.font = [UIFont systemFontOfSize:25];
                [self.contentView addSubview:_label];
                _label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
                [_label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView.mas_left).offset(100);
                    make.right.equalTo(self.contentView.mas_right).offset(-100);
                    make.bottom.equalTo(_image.mas_top).with.offset(20);
                    make.height.mas_equalTo(80);
                }];
                
            }
        } andFailureBlock:^(NSError *error) {
            
        }];
        [self.tableView.mj_header endRefreshing];
    }else {
        self.tableView.hidden = YES;
        self.label.hidden = YES;
        self.image.hidden = YES;
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logout_bg_0"]];
        _imageView.frame = CGRectMake(0, 45, SCREEN_WIDTH, SCREEN_WIDTH - 40);
        [self.contentView addSubview:_imageView];
        
    }

}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.marrOfModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJSubscriptionTableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    JYJTwoSegmentModel *model = self.marrOfModel[indexPath.row];
    [cell setModel:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super endEditing:YES];
    [_tableView setEditing:editing animated:animated];
}
#pragma mark-----delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
   JYJTwoSegmentModel *model = self.marrOfModel[indexPath.row];
    //   NSUserDefaults *userDeafults = [NSUserDefaults standardUserDefaults];
    //    NSString *name = [userDeafults objectForKey:@"userName"];
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"取消订阅" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22";
        NSString *strUid = self.dic[@"session"][@"uid"];
        NSString *str2 = @"%22%2C%22sid%22%3A%22";
        NSString *strSid = self.dic[@"session"][@"sid"];
        NSString *str3 = @"%22%7D%2C%22item_ids%22%3A%22";
        NSString *strItem_id =[NSString stringWithFormat:@"%ld",model.item_id] ;
        NSString *str4 = @"%22%2C%22operate%22%3A0%7D";
        str1 = [[[[[[str1 stringByAppendingString:strUid] stringByAppendingString:str2] stringByAppendingString:strSid] stringByAppendingString:str3] stringByAppendingString:strItem_id] stringByAppendingString:str4];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/procwatch.jhtml" andBody:str1 andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result){
            [self.marrOfModel removeObjectAtIndex:indexPath.row];
            [_tableView reloadData];
            if (self.marrOfModel.count == 0) {
                self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
                _image.frame = CGRectMake(0, 0, 150,150);
                _image.center = self.contentView.center;
                [self.contentView addSubview:_image];
                self.label = [[UILabel alloc]init];
                _label.text = @"未搜到相关内容";
                _label.textAlignment = 1;
                _label.font = [UIFont systemFontOfSize:25];
                [self.contentView addSubview:_label];
                _label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
                [_label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView.mas_left).offset(100);
                    make.right.equalTo(self.contentView.mas_right).offset(-100);
                    make.bottom.equalTo(_image.mas_top).with.offset(20);
                    make.height.mas_equalTo(80);
                }];
                
                
            }
        } andFailureBlock:^(NSError *error) {
            
        }];
        
        
        
    }];
    
    rowAction.backgroundColor = [UIColor clearColor];
    
    
    return @[rowAction];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
    JYJTwoSegmentModel *model = self.marrOfModel[indexPath.row];
    details.segmentModel = model;
    self.handelAction (details);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
